<?php
class PaymentAction extends FrontendAction
{
    public function pay_return()
    {
        $order_id = isset($_GET['order_id']) ? intval($_GET['order_id']) : 0;
        if (!$order_id) {
            // 订单无效
            $this->error('订单无效');
        }
        $order_mod = D('Order');
        $order_info = $order_mod->where('id='.$order_id)->find();
        if (!$order_info) {
            $this->error('订单无效');
        }
         include( APP_PATH . '/../payment/alipay/alipay.php' );
        $payment_config = array(
                'alipay_service' => 'create_direct_pay_by_user',
                'alipay_partner' => '2088002337270972',
                'seller_email' => 'shenqin@live.com',
                'security_code' => 'u0ky4v38f1s8fyyccyg82m29ben2g7ov',
                'sign_type' => 'MD5',
                'notify_url' => u('Payment/pay_notify'),
                'return_url' => u('Payment/pay_return'),
            );
        $payment_obj = new AlipayPayment($payment_config);
        $payment_order_info = array(
                'orderid' => $order_info['id'],
                'subject' => '果趣订单：' . $order_info['id'],
                'body' => '果趣订单：' . $order_info['id'],
                'price' => $order_info['amount'],
                'quantity' => $order_info['quantity'],
                'shipping_fee' => '0',
            );
        $notify_result = $payment_obj->verify_notify($payment_order_info);
        if ($notify_result === false) {
            $this->error('支付失败');
        }
        $this->success('支付完成', u('Member/myorder'));
    }

    public function pay_notify()
    {
        $order_id = isset($_GET['order_id']) ? intval($_GET['order_id']) : 0;
        if (!$order_id) {
            // 订单无效
            echo 'fail';
            exit;
        }
        $order_mod = D('Order');
        $order_info = $order_mod->where('id='.$order_id)->find();
        if (!$order_info) {
            echo 'fail';
            exit;
        }
        include( APP_PATH . '/../payment/alipay/alipay.php' );
        $payment_config = array(
                'alipay_service' => 'create_direct_pay_by_user',
                'alipay_partner' => '2088002337270972',
                'seller_email' => 'shenqin@live.com',
                'security_code' => 'u0ky4v38f1s8fyyccyg82m29ben2g7ov',
                'sign_type' => 'MD5',
                'notify_url' => u('Payment/pay_notify'),
                'return_url' => u('Payment/pay_return'),
            );
        $payment_obj = new AlipayPayment($payment_config);
        $payment_order_info = array(
                'orderid' => $order_info['id'],
                'subject' => '果趣订单：' . $order_info['id'],
                'body' => '果趣订单：' . $order_info['id'],
                'price' => $order_info['amount'],
                'quantity' => $order_info['quantity'],
                'shipping_fee' => '0',
            );
        $notify_result = $payment_obj->verify_notify($payment_order_info);
        if ($notify_result === false) {
            echo 'fail';
            exit;
        }

        // 通过返回信息修改订单状态
        $order_mod->change_order_status($order_id, $notify_result);
        echo 'success';
        exit;
    }
}
?>